﻿using Ruandao.MiniAccess.SqlServer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Configuration;

namespace TestMiniAccess
{


    [TestClass()]
    public class SqlDatabaseTest
    {


        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        SqlDatabase_Accessor sqlDatabase;
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
            string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
            sqlDatabase = new SqlDatabase_Accessor(connString); 

        }

        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        [DeploymentItem("Ruandao.MiniAccess.dll")]
        public void ParseAtParameterTest()
        {
            string sqlText = "select @@identity,* from master where id = '1' and type=@type and email = 'hello@qq.com' and type=@type and a<@a and about like @about or animal<=@animal or people > @people country in(@china,@usa,@uk)";
            string[] expected = {"@type","@a","@about","@animal","@people","@china","@usa","@uk",};
            string[] actual = sqlDatabase.ParseAtParameter(sqlText);

            Assert.AreEqual(8, actual.Length);
            Assert.AreEqual(expected[0], actual[0]);
            Assert.AreEqual(expected[1], actual[1]);
            Assert.AreEqual(expected[2], actual[2]);
            Assert.AreEqual(expected[3], actual[3]);
            Assert.AreEqual(expected[4], actual[4]);
            Assert.AreEqual(expected[5], actual[5]);
            Assert.AreEqual(expected[6], actual[6]);
            Assert.AreEqual(expected[7], actual[7]);
        }

        [TestMethod()]
        [DeploymentItem("Ruandao.MiniAccess.dll")]
        public void ParseFormatStringParameterTest()
        {
            string sqlText = "select @@identity,* from master where id = '1' and type={1} and email = 'hello@qq.com' and type={0} and a<{3} and type1 like {2} or type3<={4} or type3 > {5} country in({5},{6},{7})";
            string[] expected = { "{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", };
            string[] actual = sqlDatabase.ParseFormatStringParameter(sqlText);

            Assert.AreEqual(8, actual.Length);
            Assert.AreEqual(expected[0], actual[0]);
            Assert.AreEqual(expected[1], actual[1]);
            Assert.AreEqual(expected[2], actual[2]);
            Assert.AreEqual(expected[3], actual[3]);
            Assert.AreEqual(expected[4], actual[4]);
            Assert.AreEqual(expected[5], actual[5]);
            Assert.AreEqual(expected[6], actual[6]);
            Assert.AreEqual(expected[7], actual[7]);
        }
    }
}
